package venndiagram;

import java.awt.Point;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Adam
 */
public class GraphDataTwoSets implements GraphData
{
    public String aName;
    public String bName;

    public int a;
    public int b;
    public int ab;

    public CircleData maxCircle;
    public CircleData minCircle;
    public final double MAX_RADIUS;

    public GraphDataTwoSets(String aName, String bName, int a, int b, int ab)
    {
        //Set Names
        this.aName = aName;
        this.bName = bName;

        //Number of elements in each subset
        this.a = a;
        this.b = b;
        this.ab = ab;

        //Initialize max and min circles; set max radius
        if(a >= b)
        {
            //Create maxCircle based on set a
            MAX_RADIUS = Math.sqrt(a + ab / Math.PI);
            this.maxCircle = new CircleData(MAX_RADIUS, new Point(0,0), this.aName);

            //Create minCircle based on set b
           this.minCircle = new CircleData(Math.sqrt(b + ab / Math.PI), new Point(0,0), this.bName);
        }
        else
        {
            //Create maxCircle based on set b
            MAX_RADIUS = Math.sqrt(b + ab / Math.PI);
            this.maxCircle = new CircleData(MAX_RADIUS, new Point(0,0), this.bName);

           //Create minCircle based on set a
           this.minCircle = new CircleData(Math.sqrt(a + ab / Math.PI), new Point(0,0), this.aName);
        }
    }


    @Override
    //Find and set centers for all three circles
    public void findCenters()
    {
        double intToMax = ab / (MAX_RADIUS * MAX_RADIUS * Math.PI);    //Ratio of intersection between max and min circles to max circle

        //Initialize distance so the circles don't intersect
        int distance = (int) (maxCircle.getRadius() + minCircle.getRadius() + 1);

        double intersection;    //Area of intersection of max and min circles

        //Decrease distance until area of intersection at current distance is greater than previously calculated area of intersection
        do
        {
            distance--;
            intersection = findIntersection(maxCircle.getRadius(), minCircle.getRadius(), distance);
        }
        while(intersection / (MAX_RADIUS * MAX_RADIUS * Math.PI) < intToMax);

        //TODO Check if intersection from distance + 1 is more accurate than current distance

        //Set minCircle center
        minCircle.setCenter(new Point(0, (int)distance));
    }

    //Find the area of intersection for two circles
    public double findIntersection(double radius1, double radius2, int distance)
    {
        return (radius1 * radius1 * Math.acos((distance * distance + radius1 * radius1 - radius2 * radius2) / (2 * distance * radius1))) +
               (radius2 * radius2 * Math.acos((distance * distance + radius2 * radius2 - radius1 * radius1) / (2 * distance * radius2))) -
               (0.5 * Math.sqrt((radius1 + radius2 - distance) * (radius1 - radius2 + distance) * (radius2 - radius1 + distance) * (radius1 + radius2 + distance)));
    }

}